
#ifndef MYCLASSFACTORY
#define MYCLASSFACTORY

#include "..\CDir\CDir.hpp"

// Class CPP is the base for COM object (Routines)
class CClassFactory : public IClassFactory
{
public:
   // IUnknown
   STDMETHOD( QueryInterface(REFIID, void** ));
   STDMETHOD_(ULONG, AddRef());
   STDMETHOD_(ULONG, Release());
  
   // IClassFactory
   STDMETHOD( CreateInstance(LPUNKNOWN, REFIID, void**));
   STDMETHOD( LockServer(BOOL));

   CClassFactory();

protected:
   long       m_lRef;
};


CClassFactory::CClassFactory()
{
    m_lRef = 0;
}

STDMETHODIMP CClassFactory::QueryInterface( REFIID riid, void** ppv )
{
   *ppv = 0;

   if ( riid == IID_IUnknown || riid == IID_IClassFactory)
      *ppv = this;

   if ( *ppv )
   {
      AddRef();
      return S_OK;
   }

   return(E_NOINTERFACE);
}

STDMETHODIMP_(ULONG) CClassFactory::AddRef()
{
   return InterlockedIncrement( &m_lRef );
}

STDMETHODIMP_(ULONG) CClassFactory::Release()
{
   if ( InterlockedDecrement( &m_lRef ) == 0 )
   {
      delete this;
      return 0;
   }

   return m_lRef;
}

STDMETHODIMP CClassFactory::CreateInstance( LPUNKNOWN, REFIID riid, void** ppvObj )
{

   HRESULT    hr;

   *ppvObj = 0;

   // Customize
   
   CDir* pDir = new CDir(g_params[g_idTREEDIR]);   
   if (pDir == 0) return( E_OUTOFMEMORY );
   hr = pDir->QueryInterface( riid, ppvObj );
   if ( FAILED( hr ) ) delete pDir;

//   CObject*  pObj;
//   pObj = new CObject();
//   if ( pObj == 0 ) return( E_OUTOFMEMORY );
//   hr = pObj->QueryInterface( riid, ppvObj );
//   if ( FAILED( hr ) ) delete pObj;

   return hr;
}

STDMETHODIMP CClassFactory::LockServer( BOOL fLock )
{
   if ( fLock )
      InterlockedIncrement( &g_lLocks ); 
   else
      InterlockedDecrement( &g_lLocks );

    return S_OK;
}

#endif
